package org.gwt.pluginframework.desktop.client;

import org.gwt.pluginframework.common.Plugin;
import org.gwt.pluginframework.common.event.IPluginEvent;
import org.gwt.pluginframework.common.event.PluginEvent;
import org.gwt.pluginframework.common.event.PluginEventHandler;
import org.gwt.pluginframework.common.exception.PluginException;
import org.gwt.pluginframework.plugin.CustomEvents;
import org.gwt.pluginframework.plugin.api.IPage;
import org.gwt.pluginframework.plugin.api.Wizard;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class FormA extends Plugin {

	private static final long serialVersionUID = -5641378086378411803L;

	protected FormPanel rootPanel;
	
	protected TabItem item;
	
	public void setupManifest() {
		manifest.setName("FormA");
		manifest.setVersion("1.0");
	}
	
	@Override
	public void onInitialize() {
		rootPanel = new FormPanel();
		rootPanel.setBorders(false);
		rootPanel.setHeaderVisible(false);
		rootPanel.setBodyBorder(false);
		
		registerEventHandler(new PluginEventHandler(CustomEvents.START_FORM_A_EVENT) {
			public void handle(IPluginEvent event) {
				if(!isStarted()) {
					start();
					dispatch(new PluginEvent(CustomEvents.START_TAB_EVENT,
							new String[] {"tabItem"}, new Object[] {item}));
				} else {
					dispatch(new PluginEvent(CustomEvents.FOCUS_TAB_EVENT,
							new String[] {"tabItem"}, new Object[] {item}));
				}	
			}
		});
	}
	
	@Override
	public void onStart() {
		rootPanel.add(new Label("My Form A"));
		
		TextField<String> name = new TextField<String>();
		name.setFieldLabel("Name");
		name.setAllowBlank(false);
		rootPanel.add(name);
		
		TextField<String> surname = new TextField<String>();
		surname.setFieldLabel("Surname");
		surname.setAllowBlank(false);
		rootPanel.add(surname);
		
		TextField<Integer> age = new TextField<Integer>();
		age.setFieldLabel("Age");
		age.setWidth(50);
		rootPanel.add(age);
		
		rootPanel.addButton(new Button("Exception!", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				manager.handle(new PluginException(FormA.this, new IllegalStateException("Test exception!")));
			}
		}));
		rootPanel.add(new Button("Run wizard", new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				Wizard wizard = new Wizard();
				wizard.registerPage("general", new IPage() {
					Widget widget;
					TextField<String> text;
					public Widget getWiget() {
						if(widget == null) {
							ContentPanel panel = new ContentPanel();
							panel.setHeaderVisible(false);
							panel.add(new Label("Name"));
							text = new TextField<String>();
							panel.add(text);
							widget = panel;
						}
						return widget;
					}
					public boolean validate() {
						return text.getValue() == null ? false : ((String) text.getValue()).length() > 0;
					}
				}, "General", "Test general page");
				wizard.registerPage("attributes", new IPage() {
					Widget widget;
					public Widget getWiget() {
						if(widget == null) {
							ContentPanel panel = new ContentPanel();
							panel.setHeaderVisible(false);
							panel.add(new Label("Attrs"));
							widget = panel;
						}
						return widget;
					}
					public boolean validate() {
						return true;
					}
				}, "Attributes", "Test attributes page");
				wizard.create();
				wizard.start();
			}
		}));
		
		item = new TabItem();
		item.setText("FormA");
		item.setLayout(new FitLayout());
		item.add(rootPanel);
	}
}
